package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrdersettingService;
import com.itheima.utils.POIUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/*
* 预约设置
* */
@RestController
@RequestMapping("/ordersetting")
public class OrdersettingController {

    @Reference
    private OrdersettingService ordersettingService;

    //接收文件，批量增加预约设置
    @PreAuthorize("hasAuthority('ORDERSETTING')")
    @RequestMapping("/upload")
    public Result add(@RequestParam MultipartFile excelFile) throws IOException {



        try {
            List<String[]> readExcel = POIUtils.readExcel(excelFile);
           List list = new ArrayList<OrderSetting>();

            //类型转换， 将String数组转换成OrderSetting对象
            for (String[] strings : readExcel) {
                OrderSetting orderSetting = new OrderSetting();
                String orderDate  = strings[0];  //预约事件
                String number = strings[1]; //预约总人数
                orderSetting.setOrderDate(new Date(orderDate));
                orderSetting.setNumber(Integer.parseInt(number));
                list.add(orderSetting);
                }
            //System.out.println(list.get(0));

                ordersettingService.add(list);

                return new Result(true,MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        } catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }



    }

    @RequestMapping("/FindAllByDate")
    @PreAuthorize("hasAuthority('ORDERSETTING')")
    public Result FindAllByDate (String date){
       // System.out.println(date);

        try {
            List<Map> maps = ordersettingService.FindAllByDate(date);
            //System.out.println(maps);
            return new Result(true,MessageConstant.GET_ORDERSETTING_SUCCESS,maps);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_ORDERSETTING_FAIL);
        }


    }


    //设置单个日子进行设置预约次数
    @PreAuthorize("hasAuthority('ORDERSETTING')")
    @RequestMapping("/editNumberByDate")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting){
       // System.out.println(123);

        //System.out.println(orderSetting.getOrderDate());

//        try {
//            ordersettingService.editNumberByDate(orderSetting);
//            return new Result(true,MessageConstant.ORDERSETTING_SUCCESS);
//        } catch(
//            return new Result(false,MessageConstant.ORDERSETTING_FAIL);
//        )

        try {
            ordersettingService.editNumberByDate(orderSetting);
            return new Result(true,MessageConstant.ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ORDERSETTING_FAIL);
        }

    }

}
